<?php


namespace app\admin\controller;


use app\common\model\ReceiveOrderModel;
use cmf\controller\AdminBaseController;
use think\db\Query;
use think\Request;

class ReceiveController extends AdminBaseController
{
    public function index(Request $request)
    {
        $list = ReceiveOrderModel::where(function (Query $query) use ($request) {
            if (isset($request->status) && $request->status > -1) {
                $query->where('status', $request->status);
            }
            if (!empty($request->keyword)) {
                $query->where('order_nos', 'like', "%{$request->keyword}%");
            }
        })->order('id desc')->paginate();
        $this->assign('list', $list);
        return $this->fetch();
    }

    public function del($id)
    {
        if (ReceiveOrderModel::destroy($id)) {
            $this->success('ok');
        } else {
            $this->error('失败');
        }
    }

    public function handle(Request $request)
    {
        $data = ReceiveOrderModel::where('id', $request->id)->find();
        if ($request->isPost()) {
            if (empty($request->images)) {
                $this->error('凭证不能为空');
            }
            $param = [
                'images' => json_encode($request->images),
                'status' => 1,
                'handle_time' => time()
            ];
            if ($data->save($param)) {
                $this->success('ok', 'see?id=' . $request->id);
            } else {
                $this->error('失败');
            }
        }
        return $this->fetch('handle', ['data' => $data]);
    }

    public function see($id)
    {
        $data = ReceiveOrderModel::find($id);
        if (!empty($data['images'])) {
            $data['images'] = json_decode($data['images']);
        }
        return $this->fetch('see', ['data' => $data]);
    }

}